
/***************************************************************************************
****************************************************************************************
* FILE		: main.c
* Description	: 
*			  
* Copyright (c) 2020 by Simlife. All Rights Reserved.
* 
* History:
* Version		Name		Date				Description
   0.1		Alen			2020/05/07		Initial Version
   
****************************************************************************************
****************************************************************************************/

#define GLOBAL_EXT_
#include "includes.h"

void sleepCheck(void);
void vibrationHadle(void);
void systemModeHadle(void);

void init(void)
{
	initMcu();
	initRam();

	initChargeCheckControlPin();
	initVibrationControlPin();
	initHotCompressControlPin();
	initLEDControlPin();
	initGreenNumberPin();
	initKeyControlPin();
	initWifiControlPin();
	closeUARTPin();
}
void main(void)
{
	U8 key;
	STOP_WATCH_DOG();
	STOP_ALL_INTERRUPT();
	init();
	START_ALL_INTERRUPT();
	START_WATCH_DOG();
	while (1)
	{
		bt_receive_data_process();
		if (flag200us)
		{
			flag200us = FALSE;
			if (flagSystemStatus)
			{
				vibrationPWMProcess();
				hotCompressPWMProcess();
			}
		}

		if (flag10ms)
		{
			CLEAN_WATCH_DOG();
			flag10ms = FALSE;

			if (!flagSystemCharge)
			{
				closeGreenNumberPin();
				initKeyControlPin();
				key = keyScan();
				keyProcess(key);
				key = NULL_KEY;
			}
			chargeCheckProcess();
			sleepCheck();

			if (flagSystemStatus)
			{
				if (flagModeChange)
				{
					initGreenLRControlPin();
					greenProcess();
				}
				if (stBtInfo.overtime != 0)
				{
					stBtInfo.overtime--;
					if (stBtInfo.overtime == 0)
					{
						stBtInfo.state = START;
					}
				}
			}
		}

		if (flag250ms)
		{
			flag250ms = FALSE;
			if (flagSystemStatus)
			{
				systemLEDProcess();
				batteryAdcProcess();
				vibrationHadle();
				bt_data_send_automatic();
			}
		}

		if (flag500ms)
		{
			flag500ms = FALSE;
			if (flagSystemStatus)
			{
				controlCloseSystem();
				systemModeHadle();
				hotCompressProcess();
			}
			else
			{
				chargeLEDProcess();
			}
		}
	}
}

void vibrationHadle(void)
{
	if (stSystem.systemMode == OMNIPOTENT_MODE)
	{
		if (!flagModeChange)
		{
			vibrationProcess();
		}
	}
}

void systemModeHadle(void)
{
	if (flagModeChange)
	{
		stTime.countModeChangeTimes--;
		if (stTime.countModeChangeTimes == 0)
		{
			flagModeChange = FALSE;
			stTime.countModeChangeTimes = stSystem.systemMode == OMNIPOTENT_MODE ? 120 : 60;
			closeGreenNumberPin();
			stTime.countGreenStep = 0;
		}
	}
	else
	{
		if (flagAllMode)
		{
			vibrationProcess();
		}
		else
		// if (stSystem.systemMode != RELAX_MODE)
		{
			stTime.countModeChangeTimes--;
			if (stTime.countModeChangeTimes == 0)
			{
				flagModeChange = TRUE;
				stTime.countModeChangeTimes = 120;
				if (stSystem.systemMode == OMNIPOTENT_MODE)
				{
					fillBufferWithValue((U8 *)(&stVibration), sizeof(stVibration), 0);
					closeVibrationAllPin();
				}
			}
		}
	}
}

void sleepCheck(void)
{
	static U8 powerStatus = FALSE;
	if (flagSystemStatus)
		return;

	if ((!key1PortAndPin) || chargeCheckPortAndPin)
		return;

	stTime.countReadyClose--;
	if (stTime.countReadyClose > 0)
	{
		return;
	}
	initKeyControlPin();
	initCloseAdc();

	closeWifiControlPin();

	STOP_UART();
	closeUARTPin();

	STOP_WATCH_DOG();
	STOP_TIMER2();
	START_ALL_INTERRUPT();
	closeLEDPin();
	START_INT0();
	_nop();
	_nop();
	_nop();
	_nop();
	_idlen = 0;
	_halt();
	_nop();
	_nop();
	_nop();
	_nop();
	_nop();
	STOP_INT0();
	initAdc();
	if (flagSystemLowPower)
	{
		powerStatus = TRUE;
	}
	else
	{
		powerStatus = FALSE;
	}

	initRam();
	flagSystemLowPower = powerStatus;
	stTime.countReadyClose = GOTO_SLEEP_TIME;
	START_TIMER2();
	START_ALL_INTERRUPT();
	START_WATCH_DOG();
	CLEAN_WATCH_DOG();
	chargeCheckProcess();
	initGreenNumberPin();
}
